/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * Project Name: lmrp-android app
 * Create Time: 16-2-16 下午6:43
 */

package com.lenovo.mso2o.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lenovo.mso2o.R;
import com.lenovo.mso2o.entity.Shortcut;
import com.lenovo.mso2o.ui.fragment.ShortcutSettingFragment;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * MainFragment 中GridView的适配器
 * Created by lit on 2015/11/11.
 */
public class ShortcutAdapter extends RecyclerView.Adapter<ShortcutAdapter.ViewHolder> {
    private Context mContext;
    private List<Shortcut> typelist;
    private OnShortcutSelectedListener listener;

    public ShortcutAdapter(Context context) {
        this.mContext = context;
        if(typelist != null && typelist.size() < 8){
            int size = typelist.size();
            for(int i = 0; i< 8 - size; i++){
                typelist.add(new Shortcut(-1));
            }
        }
    }

    public void setList(List<Shortcut> newList) {
        this.typelist = newList;
        this.notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View view = inflater.inflate(R.layout.mainfragment_gridview_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (position==0) {
            holder.layoutBg.setBackgroundResource(R.drawable.home_page_bottom_btn_bg_left_top_shape);
        } else if (position==3) {
            holder.layoutBg.setBackgroundResource(R.drawable.home_page_bottom_btn_bg_right_top_shape);
        }else if (position==4) {
            holder.layoutBg.setBackgroundResource(R.drawable.home_page_bottom_btn_bg_left_bottom_shape);
        } else if (position==7) {
            holder.layoutBg.setBackgroundResource(R.drawable.home_page_bottom_btn_bg_right_bottom_shape);
        }else {
            holder.layoutBg.setBackgroundResource(R.drawable.home_page_bottom_btn_bg_middle_shape);
        }
        if (typelist.get(position).getId() == ShortcutSettingFragment.SCHEME_QUERY) {
            holder.gridItemLayout.setVisibility(View.VISIBLE);
            holder.addImageV.setVisibility(View.GONE);
            holder.imageV.setImageResource(R.drawable.home_page_precept_query);
            holder.textV.setText(mContext.getString(R.string.scheme_query));
        } else if (typelist.get(position).getId() == ShortcutSettingFragment.PRODUCTDESCRIPTION) {
            holder.gridItemLayout.setVisibility(View.VISIBLE);
            holder.addImageV.setVisibility(View.GONE);
            holder.imageV.setImageResource(R.drawable.home_page_product_description);
            holder.textV.setText(mContext.getString(R.string.productdescription));
        } else if (typelist.get(position).getId() == ShortcutSettingFragment.FASTBILLING) {
            holder.gridItemLayout.setVisibility(View.VISIBLE);
            holder.addImageV.setVisibility(View.GONE);
            holder.imageV.setImageResource(R.drawable.auto_check);
            holder.textV.setText(mContext.getString(R.string.hunting));
        } else if (typelist.get(position).getId() == ShortcutSettingFragment.MYMESSAGE) {
            holder.gridItemLayout.setVisibility(View.VISIBLE);
            holder.addImageV.setVisibility(View.GONE);
            holder.imageV.setImageResource(R.drawable.home_page_my_news);
            holder.textV.setText(mContext.getString(R.string.hunting));
        } else if (typelist.get(position).getId() == ShortcutSettingFragment.INITIATECHAT) {
            holder.gridItemLayout.setVisibility(View.VISIBLE);
            holder.addImageV.setVisibility(View.GONE);
            holder.imageV.setImageResource(R.drawable.actionbar_addnew);
            holder.textV.setText(mContext.getString(R.string.initiatechat));
        }else if (typelist.get(position).getId() == ShortcutSettingFragment.HANDYHAVERSACK) {
            holder.gridItemLayout.setVisibility(View.VISIBLE);
            holder.addImageV.setVisibility(View.GONE);
            holder.imageV.setImageResource(R.drawable.home_page_bag);
            holder.textV.setText(mContext.getString(R.string.handyhaversack));
        } else if (typelist.get(position).getId() == ShortcutSettingFragment.ADDFUNCTION) {
            holder.gridItemLayout.setVisibility(View.GONE);
            holder.addImageV.setVisibility(View.VISIBLE);
        } else if (typelist.get(position).getId() == ShortcutSettingFragment.ADDFUNCTION) {
            holder.imageV.setImageResource(R.drawable.home_page_add);
            holder.textV.setVisibility(View.GONE);
        }else{
            holder.gridItemLayout.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return typelist.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @Bind(R.id.grid_item_image)
        ImageView imageV;
        @Bind(R.id.grid_item_image_add)
        ImageView addImageV;
        @Bind(R.id.grid_item_text)
        TextView textV;
        @Bind(R.id.grid_item_bg)
        RelativeLayout layoutBg;
        @Bind(R.id.grid_item)
        LinearLayout gridItemLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            int position = getAdapterPosition();
            if (listener != null) {
                Shortcut shortcut = typelist.get(position);
                listener.onShortcutSelected(shortcut, position);
            }
        }
    }

    public static interface OnShortcutSelectedListener {
        void onShortcutSelected(Shortcut shortcut, int position);
    }

    public void setOnShortcutSelectedListener(OnShortcutSelectedListener listener) {
        this.listener = listener;
    }
}
